﻿using _8Group.MES.ISongService.WmItemRecpt;
using _8Group.MES.ISongService.WmItemRecpt.DTO;
using _8Group.MES.ISongService.WMProductRecpt;
using _8Group.MES.ISongService.WMProductRecpt.DTO;
using _8Group.MES.Model.MD主数据;
using _8Group.MES.Model.PRO生产管理;
using _8Group.MES.Model.WM仓储管理;
using FourGroups.Project;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace _8Group.MES.SongService
{
    //  [ApiExplorerSettings(GroupName = "宋恒漳")]
    public class WmProductRecptService : ApplicationService, IWmProductRecptService
    {
        private readonly IRepository<WM_Product_Recpt, int> _productRecptRepository;
        private readonly IRepository<Pro_Workorder, int> _workorderRepository;
        public WmProductRecptService(IRepository<WM_Product_Recpt, int> productRecptRepository, IRepository<Pro_Workorder, int> workorderRepository)
        {
            _productRecptRepository = productRecptRepository;
            _workorderRepository = workorderRepository;
        }
        public async Task<object> GetProductRecptListAsync()
        {
            return await _productRecptRepository.GetListAsync();
        }
        /// <summary>
        /// 获取产品入库列表
        /// </summary>
        /// <param name="productRecptDTO"></param>
        /// <returns></returns>
        public async Task<object> GetProductRecptListAsync(RequestWmProductRecptDTO productRecptDTO)
        {
            var productRecptList = await _productRecptRepository.GetQueryableAsync();
            //和物料产品表关联
            var mditem = await _workorderRepository.GetQueryableAsync();
            var joinedList = from product in productRecptList
                             join item in mditem on product.WorkorderCode equals item.WorkorderCode
                             select new
                             {
                                 product.RecptCode,
                                 product.RecptName,
                                 product.WorkorderId,
                                 product.RecptDate,
                                 product.Status,
                                 product.Remark,
                                 product.WorkorderCode,
                                 product.WorkorderName,
                                 product.CreateBy,
                                 product.CreateTime,
                                 product.LocationCode,
                                 product.WarehouseCode,
                                 product.AreaName,
                                 product.Id,
                                 item.ProduceCode,
                                 item.ProduceName,
                                 item,
                                 product,

                             };
            //条件查询  
            if (!string.IsNullOrEmpty(productRecptDTO.RecptName))
            {
                joinedList = joinedList.Where(x => x.product.RecptName.Contains(productRecptDTO.RecptName));
            }
            if (!string.IsNullOrEmpty(productRecptDTO.RecptCode))
            {
                joinedList = joinedList.Where(x => x.product.RecptCode.Contains(productRecptDTO.RecptCode));
            }
            if (!string.IsNullOrEmpty(productRecptDTO.WarehouseName))
            {
                joinedList = joinedList.Where(x => x.product.WarehouseName.Contains(productRecptDTO.WarehouseName));
            }
            if (!string.IsNullOrEmpty(productRecptDTO.WorkorderCode))
            {
                joinedList = joinedList.Where(x => x.product.WorkorderCode.Contains(productRecptDTO.WorkorderCode));
            }
            var count = joinedList.Count();
            joinedList = joinedList.OrderBy(x => x.product.Id).Skip(((productRecptDTO.Page - 1) ?? 0) * (productRecptDTO.Limit ?? 10)).Take(productRecptDTO.Limit ?? 10);
            var result = new ResponseModel
            {
                code = "",
                count = count,
                data = joinedList.ToList(),
                msg = "",
            };
            return result;
            //productRecptList = productRecptList.WhereIf(productRecptDTO.RecptName != null, x => x.RecptName.Contains(productRecptDTO.RecptName));
            //productRecptList = productRecptList.WhereIf(productRecptDTO.RecptCode != null, x => x.RecptCode.Contains(productRecptDTO.RecptCode));
            //productRecptList = productRecptList.WhereIf(productRecptDTO.WarehouseName != null, x => x.WarehouseName.Contains(productRecptDTO.WarehouseName));
            //productRecptList = productRecptList.WhereIf(productRecptDTO.WorkorderCode != null, x => x.WorkorderCode.Contains(productRecptDTO.WorkorderCode));
            //var count = productRecptList.Count();
            //productRecptList = productRecptList.OrderBy(x => x.Id).Skip(((productRecptDTO.Page - 1) ?? 0) * (productRecptDTO.Limit ?? 10)).Take(productRecptDTO.Limit ?? 10);
            //var result = new ResponseModel
            //{
            //    code = "",
            //    count = count,
            //    data = productRecptList.ToList(),
            //    msg = "",
            //};
            //return result;

        }

        /// <summary>
        /// 删除产品入库
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> DeleteProductRecptAsync(int id)
        {
            var productRecpt = await _productRecptRepository.GetAsync(id);
            await _productRecptRepository.DeleteAsync(productRecpt, true);
            return true;
        }

        /// <summary>
        /// 新增产品入库
        /// </summary>
        /// <param name="createWmProductRecptDTO"></param>
        /// <returns></returns>
        public async Task<bool> AddProductRecptAsync(AddWmProductRecptDTO createWmProductRecptDTO)
        {
            //判断产品是否存在
            var productRecpt = await _productRecptRepository.FirstOrDefaultAsync(x => x.RecptName == createWmProductRecptDTO.RecptName);
            if (productRecpt != null)
            {
                return false;
            }
            createWmProductRecptDTO.CreateBy = "宋恒漳";
            createWmProductRecptDTO.CreateTime = DateTime.Now;
            var wmProductRecpt = AutoMapperExt.MapTo<WM_Product_Recpt>(createWmProductRecptDTO);
            await _productRecptRepository.InsertAsync(wmProductRecpt);
            return true;
        }

        /// <summary>
        /// 更新产品入库
        /// </summary>
        /// <param name="updateWmProductRecptDTO"></param>
        /// <returns></returns>
        public async Task<bool> UpdateProductRecptAsync(UpdateWmProductRecptDTO updateWmProductRecptDTO)
        {
            updateWmProductRecptDTO.UpdateBy=   "宋恒漳";
            updateWmProductRecptDTO.UpdateTime = DateTime.Now;
            var wmItemRecpt = AutoMapperExt.MapTo<WM_Product_Recpt>(updateWmProductRecptDTO);
            await _productRecptRepository.UpdateAsync(wmItemRecpt);
            return true;
        }

        /// <summary>
        /// 更新产品入库状态
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
       public async Task<bool> UpdateProductRecptStatusAsync(int id)
        {
            //查询ID对应的采购入库
            var wmItemRecpt = await _productRecptRepository.GetAsync(id);
            if (wmItemRecpt == null)
            {
                return false;
            }
            //判断采购入库状态
            if (wmItemRecpt.Status == "PREPARE")
            {
                wmItemRecpt.Status = "Completed";
            }
            else
            {
                wmItemRecpt.Status = "PREPARE";
            }
            await _productRecptRepository.UpdateAsync(wmItemRecpt);
            return true;
        }
    }
}
